using Godot;
using System;
using System.Collections.Generic;
using System.Runtime;
using System.Threading.Tasks;

public partial class SceneManager
{
    private static SceneManager _instance = null;
    public static SceneManager getInstance()
    {
        if (_instance == null)
        {
            _instance = new SceneManager();
        }
        return _instance;
    }



    //打开场景
    public void enterScene(SceneId sceneId)
    {

        List<WindowBase> viewList = new List<WindowBase>();
        List<WindowBase> destoryList = new List<WindowBase>();
        if (SceneUtil.currentScene is not null)
        {
            if (SceneUtil.currentScene.Name == sceneId.ToString())
            {
                //场景已经打开不做处理
                return;
            }

            //获取当前节点上非指定场景的页面
            var viewRootNode = ViewManager.getInstance().viewRootNode;

            foreach (var view in viewRootNode.GetChildren())
            {
                if (ViewConfig.viewDict[view.Name].sceneId == (int)SceneId.allScene)
                {

                    viewList.Add((WindowBase)view);
                }
                else
                {
                    destoryList.Add((WindowBase)view);
                }
                ViewManager.getInstance().close(view.Name);
            }

        }
        //todo 打开加载页面
        ViewManager.getInstance().open("LoadingView");

        //打开新的场景（异步加载）
        var path = SceneUtil.getScenePath(sceneId);
        var err = ResourceLoader.LoadThreadedRequest(path);
        var sceneConfig = SceneUtil.GetSceneConfig(sceneId);
        GD.PrintErr(err);
        while (true)
        {
            var status = ResourceLoader.LoadThreadedGetStatus(path);
            if (status == ResourceLoader.ThreadLoadStatus.Loaded)
            {
                var sceneFile = (PackedScene)ResourceLoader.LoadThreadedGet(path);
                var scene = (SceneBase)sceneFile.Instantiate();
                scene.Name = sceneConfig.sceneName;
                scene.sceneId = sceneId;
                scene.sceneType = (SceneType)sceneConfig.sceneTypeId;

                /*var isHas = scene.HasNode("ViewNode");
                var viewNode = new Node2D();
                if (!isHas)
                {

                    viewNode.Name = "ViewNode";
                    viewNode.ZIndex = 99;   //调整层级到最大
                    scene.AddChild(viewNode);
                }
                else
                {
                    viewNode = scene.GetNode<Node2D>("ViewNode");
                }



                //转移所有场景界面ID
                if (viewList.Count > 0)
                {
                    foreach (var view in viewList)
                    {
                        view.Reparent(viewNode);
                    }
                }*/
                SceneUtil.ChangeScene(scene);
                DateTime nowTimer = DateTime.Now;
                int interval = 0;
                while (interval < 1000)
                {
                    TimeSpan spand = DateTime.Now - nowTimer;
                    interval = spand.Seconds;
                }


                ViewManager.getInstance().close("LoadingView");



                break;
            }
        }
        //this.root.Root.RemoveChild(this.currentScene);

        //this.currentScene.Free();  //销毁场景
    }

    public void enterNextScene()
    {

    }


}
